package com.dongdongshop.controller;

import cn.hutool.core.util.StrUtil;
import com.dongdongshop.common.AjaxResult;
import com.dongdongshop.common.ResultEnum;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.*;
import com.dongdongshop.vo.MyOrder;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("address")
public class AddRessController {
    Logger logger = LoggerFactory.getLogger(AddRessController.class);
    @DubboReference
    private ProvincesService provincesService;//省份
    @DubboReference
    private CitiesService citiesService;//城市
    @DubboReference
    private AreasService   areasService;//区域
    @DubboReference
    private AddressService   tbAddress;//用户地址
    @DubboReference
    private OrderItemService orderItemService;
    @DubboReference
    private OrderService     orderService;
    /*省份表查询*/
    @RequestMapping("getProvinces")
    @ResponseBody
    public AjaxResult getProvinces(){
        try {
            List<TbProvinces> provincesList = provincesService.selectByAll();
            if (provincesList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(provincesList);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("省份查询失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    @RequestMapping("getCities")
    @ResponseBody
    public AjaxResult getCities(String id){
        if (StrUtil.isBlank(id)){
            return AjaxResult.ERROR();
        }
        try {
            List<TbCities> citiesList = citiesService.selectByProvincesId(id);
            if (citiesList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(citiesList);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("城市查询失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    @RequestMapping("getAreas")
    @ResponseBody
    public AjaxResult getAreas(String id){
        if (StrUtil.isBlank(id)){
            return AjaxResult.ERROR();
        }
        try {
            List<TbAreas> areasList = areasService.selectByCityId(id);
            if (areasList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(areasList);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("区域查询失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    /*跳转个人中心,地址管理*/
    @RequestMapping("runSettingAddress")
    private String runSettingAddress(Model model){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        model.addAttribute("userName",user.getUsername());
        return "home-setting-address";
    }
    /*查询用户的地址*/
    @RequestMapping("getAddress")
    @ResponseBody
    public AjaxResult getAddress(){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        try {
            List<TbAddress> addressList = tbAddress.selectAddressByUserId(user.getUsername());
            if (addressList.size()>0){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(addressList);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("用户地址查询失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    /*更具联系人称查询地址信息*/
    @RequestMapping("getAddressById")
    @ResponseBody
    public AjaxResult getAddressById(Long id){
        try {
            TbAddress address = tbAddress.selectById(id);
            if (address != null){
                return AjaxResult.R(ResultEnum.SUCCESS).setData(address);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("用户地址查询失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    /*用户的地址添加*/
    @RequestMapping("addAddress")
    @ResponseBody
    public AjaxResult addAddress(TbAddress address){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        if (address == null){
            return AjaxResult.ERROR();
        }
        try {
            address.setUserId(user.getUsername());
            address.setCreateDate(new Date());
            int i = tbAddress.addAddress(address);
            if (i>0){
                return AjaxResult.R(ResultEnum.SUCCESS);
            }
            return AjaxResult.R(ResultEnum.ERROR);
        }catch (Exception e){
            logger.error("用户地址添加失败,原因:{}",e);
            return AjaxResult.ERROR();
        }
    }
    /*跳转查看订单页面*/
    @RequestMapping("myOrder")
    public String myOrder(String orderNum, String tradeNo, Model model){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        List<MyOrder> myOrderList = new ArrayList<>();
        /*查询该账号下的全部订单*/
        List<TbOrder> orderList = orderService.selectByUserName(user.getUsername());
        if (orderList== null){
            return "home-index";
        }
        /*更具order_id查询tb_order_item表*/
        for (TbOrder tbOrder : orderList) {
            MyOrder myOrder = new MyOrder();
            List<TbOrderItem> orderItemList = orderItemService.selectByOrderId(tbOrder.getOrderId());
            myOrder.setOrder(tbOrder);
            myOrder.setOrderItemList(orderItemList);
            myOrderList.add(myOrder);
        }
        model.addAttribute("list",myOrderList);
        return "home-index";
    }
    /*跳转查看订单页面,查看全部已支付,未评价的订单*/
    @RequestMapping("myOrderNotEvaluated")
    public String myOrderNotEvaluated(String orderNum, String tradeNo, Model model){
        TbUser user = (TbUser)SecurityUtils.getSubject().getPrincipal();
        List<MyOrder> myOrderList = new ArrayList<>();
        /*查询该账号下的全部全部已支付,未评价的订单*/
        List<TbOrder> orderList = orderService.selectByUserName(user.getUsername());
        if (orderList== null){
            return "home-order-evaluate";
        }
        /*更具order_id查询tb_order_item表*/
        for (TbOrder tbOrder : orderList) {
            MyOrder myOrder = new MyOrder();
            List<TbOrderItem> orderItemList = orderItemService.selectByOrderId(tbOrder.getOrderId());
            myOrder.setOrder(tbOrder);
            myOrder.setOrderItemList(orderItemList);
            myOrderList.add(myOrder);
        }
        model.addAttribute("list",myOrderList);
        return "home-order-evaluate";
    }
}
